package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Entity.Apply;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//修改学生信息
public class StuApply_detailsActivity extends AppCompatActivity {

    private String TAG = "StuApply_detailsActivity测试";

    private TextView stu_apply_details_StuId;
    private TextView stu_apply_details_StuName;
    private TextView stu_apply_details_StuNational;
    private TextView stu_apply_details_StuSex;
    private TextView stu_apply_details_StuAge;
    private TextView stu_apply_details_StuPoliticalLandscape;
    private TextView stu_apply_details_StuNativePlace;
    private TextView stu_apply_details_Stucarid;
    private TextView stu_apply_details_StuPhone;
    private TextView stu_apply_details_StuEmail;
    private TextView stu_apply_details_StuClassId;
    private TextView stu_apply_details_StubackTime;
    private Button stu_apply_details_btn1;
    private Button stu_apply_details_btn2;
    private String ids;

    private LinearLayout stu_apply_details_operationlinear;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_apply_details);
        initid();
        Intent intent = getIntent();
        ids = intent.getStringExtra("id");
        Apply apply = (Apply) intent.getSerializableExtra("apply");
        find(apply);
        if (ids.equals("1")){
            stu_apply_details_operationlinear.setVisibility(View.GONE);
        }

        stu_apply_details_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ids.equals("6")){
                    deptty(apply,1);
                }else {
                    teaty(apply,1);
                }
            }
        });
        stu_apply_details_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ids.equals("6")){
                    deptty(apply,2);
                }else {
                    teaty(apply,2);
                }
            }
        });
    }

    private void initid(){
        stu_apply_details_StuId = (TextView) findViewById(R.id.stu_apply_details_StuId);
        stu_apply_details_StuName = (TextView) findViewById(R.id.stu_apply_details_StuName);
        stu_apply_details_StuNational = (TextView) findViewById(R.id.stu_apply_details_StuNational);
        stu_apply_details_StuSex = (TextView) findViewById(R.id.stu_apply_details_StuSex);
        stu_apply_details_StuAge = (TextView) findViewById(R.id.stu_apply_details_StuAge);
        stu_apply_details_StuPoliticalLandscape = (TextView) findViewById(R.id.stu_apply_details_StuPoliticalLandscape);
        stu_apply_details_StuNativePlace = (TextView) findViewById(R.id.stu_apply_details_StuNativePlace);
        stu_apply_details_Stucarid = (TextView) findViewById(R.id.stu_apply_details_Stucarid);
        stu_apply_details_StuPhone = (TextView) findViewById(R.id.stu_apply_details_StuPhone);
        stu_apply_details_StuEmail = (TextView) findViewById(R.id.stu_apply_details_StuEmail);
        stu_apply_details_StuClassId = (TextView) findViewById(R.id.stu_apply_details_StuClassId);
        stu_apply_details_StubackTime = (TextView) findViewById(R.id.stu_apply_details_StubackTime);
        stu_apply_details_btn1 = (Button) findViewById(R.id.stu_apply_details_btn1);
        stu_apply_details_btn2 = (Button) findViewById(R.id.stu_apply_details_btn2);

        stu_apply_details_operationlinear = (LinearLayout) findViewById(R.id.stu_apply_details_operationlinear);
    }

    //查询之前和修改有和不同
    private void find(Apply apply){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Student/GetStudent?StuId="+apply.getApplicantsId())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String message = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(apply.getInfoJson());
                    JSONObject jsonObject1 = new JSONObject(message);
                    JSONObject jsonObject2 = jsonArray.getJSONObject(0);
                    Log.d("jsonObject1",jsonObject1.toString());
                    Log.d("jsonObject2",jsonObject2.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                String StuId = jsonObject1.getString("StuId");
                                stu_apply_details_StuId.setText("学号为"+StuId+"的申请表");
                                stu_apply_details_StuName.setText(jsonObject2.getString("StuName"));
                                stu_apply_details_StuNational.setText(jsonObject2.getString("National"));
                                stu_apply_details_StuSex.setText(jsonObject2.getString("StuSex"));
                                stu_apply_details_StuAge.setText(jsonObject2.getString("StuAge"));
                                stu_apply_details_StuPoliticalLandscape.setText(jsonObject2.getString("PoliticalLandscape"));
                                stu_apply_details_StuNativePlace.setText(jsonObject2.getString("NativePlace"));
                                stu_apply_details_Stucarid.setText(jsonObject2.getString("StuCarid"));
                                stu_apply_details_StuPhone.setText(jsonObject2.getString("StuPhone"));
                                stu_apply_details_StuEmail.setText(jsonObject2.getString("StuEmail"));
                                stu_apply_details_StuClassId.setText(jsonObject2.getString("ClassId"));
                                stu_apply_details_StubackTime.setText(jsonObject2.getString("StubackTime"));
                                if (jsonObject1.getString("StuName").equals(jsonObject2.getString("StuName"))){
                                    stu_apply_details_StuName.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuName.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("National").equals(jsonObject2.getString("National"))){
                                    stu_apply_details_StuNational.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuNational.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("StuSex").equals(jsonObject2.getString("StuSex"))){
                                    stu_apply_details_StuSex.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuSex.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("StuAge").equals(jsonObject2.getString("StuAge"))){
                                    stu_apply_details_StuAge.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuAge.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("PoliticalLandscape").equals(jsonObject2.getString("PoliticalLandscape"))){
                                    stu_apply_details_StuPoliticalLandscape.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuPoliticalLandscape.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("NativePlace").equals(jsonObject2.getString("NativePlace"))){
                                    stu_apply_details_StuNativePlace.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuNativePlace.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("StuCarid").equals(jsonObject2.getString("StuCarid"))){
                                    stu_apply_details_Stucarid.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_Stucarid.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("StuPhone").equals(jsonObject2.getString("StuPhone"))){
                                    stu_apply_details_StuPhone.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuPhone.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("StuEmail").equals(jsonObject2.getString("StuEmail"))){
                                    stu_apply_details_StuEmail.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuEmail.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("ClassId").equals(jsonObject2.getString("ClassId"))){
                                    stu_apply_details_StuClassId.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StuClassId.setTextColor(Color.parseColor("#ff0000"));
                                }
                                if (jsonObject1.getString("StubackTime").equals(jsonObject2.getString("StubackTime"))){
                                    stu_apply_details_StubackTime.setTextColor(Color.parseColor("#000000"));
                                }else {
                                    stu_apply_details_StubackTime.setTextColor(Color.parseColor("#ff0000"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //老师同意或拒绝
    private void teaty(Apply apply,int status){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    MediaType mediaType = MediaType.parse("application/json");
                    RequestBody requestBody = RequestBody.create(mediaType,"");
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/TheApplicationForm/AlterStuCounselorApproval?token="+Sto.teachers.getToken()+"&status="+status+"&applicationFormId="+apply.getId())
                            .put(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    Log.d("msg",msg);
                    Log.d("applicationFormId",apply.getId()+"");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (msg.equals("1")){
                                if (status==1){
                                    Toast.makeText(StuApply_detailsActivity.this,"同意成功",Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(StuApply_detailsActivity.this,"拒绝成功",Toast.LENGTH_SHORT).show();
                                }
                                finish();
                            }else {
                                Toast.makeText(StuApply_detailsActivity.this,msg,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //教务处同意或拒绝
    private void deptty(Apply apply,int status){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("Id",apply.getId());
                    jsonObject1.put("ApplicantsId",apply.getApplicantsId());
                    jsonObject1.put("InfoJson",apply.getInfoJson());
                    jsonObject1.put("ByTheTime",apply.getByTheTime());
                    jsonObject1.put("ApplicationType",apply.getApplicationType());
                    jsonObject1.put("Status",apply.getStatus());

                    MediaType mediaType = MediaType.parse("application/json-patch+json");
                    RequestBody requestBody = RequestBody.create(mediaType,jsonObject1.toString()+"");
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/TheApplicationForm/AlterStuDeptApproval?token="+Sto.dept.getToken()+"&status="+status)
                            .put(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    Log.d(TAG+"msg",msg);
                    JSONArray jsonArray = new JSONArray(msg);
                    JSONObject jsonObject2 = jsonArray.getJSONObject(0);
                    String Id1 = jsonObject2.getString("Id");
                    String message1 = jsonObject2.getString("message");
                    JSONObject jsonObject22 = new JSONObject(message1);
                    int message11 = jsonObject22.getInt("message");
                    JSONObject jsonObject3 = jsonArray.getJSONObject(1);
                    String Id2 = jsonObject3.getString("Id");
                    String message2 = jsonObject3.getString("message");
                    JSONArray jsonArray1 = new JSONArray(message2);
                    JSONObject jsonObject33 = jsonArray1.getJSONObject(0);
                    String message22 = jsonObject33.getString("message");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String result = "";
                            if (message11>0){
                                result += Id1+"成功，";
                            }else {
                                result += Id1+"失败，";
                            }
                            if (message22.equals("error")){
                                result = result+Id2+"失败";
                            }else if (message22.equals("success")){
                                result = result+Id2+"成功";
                            }else {
                                result = result+Id2+message22;
                            }
                            Toast.makeText(StuApply_detailsActivity.this,result,Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}